package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 刷题分类controller
 * @return:
 * @Author:  sky
 * @date:  2024/11/7 0:01
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Autowired
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){

        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            // guava提供的断言检测功能，不匹配（false）则会直接抛出异常
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"题目分类不能为空");
            // 字符串需要同时检测， null 和 空串 两种情况，断言匹配false抛出异常
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            // alt + enter 快速生成返回变量
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * @Description:  查询 大分类 信息
     * @return: 
     * @Author:  sky
     * @date:  2024/11/16 23:28
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            // guava提供的断言检测功能，不匹配（false）则会直接抛出异常
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"题目分类类别不能为空");
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
                    .convertBoListToDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            return Result.fail("查询大分类失败");
        }

    }

    /**
     * @Description: 查询 大分类下 的 小分类
     * @return:
     * @Author:  sky
     * @date:  2024/11/16 23:28
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"大分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
                    .convertBoListToDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            return Result.fail("查询大分类下的小分类失败");
        }
    }
    
    /**
     * @Description: 更新分类
     * @return: 
     * @Author:  sky
     * @date:  2024/11/7 19:58
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);

            return Result.ok(result);
        }catch (Exception e){
            return Result.fail("更新分类失败");
        }
    }

    /**
     * @Description: 删除分类
     * @return:
     * @Author:  sky
     * @date:  2024/11/7 20:32
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok(result);
        }catch (Exception e){
            return Result.fail("删除分类失败");
        }
    }


    /**
     * @Description:  一次性查询分类 以及 标签
     * @return:
     * @Author:  sky
     * @date:  2024/11/17 19:07
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);

            List<SubjectCategoryDTO> subjectCategoryDTOList = new ArrayList<>();
            subjectCategoryBOList.forEach(subjectCategoryBO1 -> {
                SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.convertBOToDTO(subjectCategoryBO1);
                List<SubjectLabelDTO> subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE.convertBoListToDTOList(subjectCategoryBO1.getLabelBOList());
                dto.setLabelDTOList(subjectLabelDTOList);
                subjectCategoryDTOList.add(dto);
            });
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            return Result.fail("查询分类以及标签失败");
        }

    }

}
